/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-6 下午3:20
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.activities;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.base.BaseActivity;
import com.example.dqhl.jdmetro.config.Config;
import com.example.dqhl.jdmetro.utils.Dlog;
import com.example.dqhl.jdmetro.utils.JsonUtils;
import com.example.dqhl.jdmetro.utils.JumpItent;
import com.example.dqhl.jdmetro.utils.MyCallBackTakePhoto;
import com.example.dqhl.jdmetro.utils.Toastutils;
import com.example.dqhl.jdmetro.view.PictureHandlePopupWindow;
import com.example.dqhl.jdmetro.view.PictureSelectPopupWindow;
import com.jph.takephoto.model.TResult;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * description:意见反馈
 * autour:guanluocang
 * date:17/7/6 15:20
 */
public class MyCallBackActivity extends BaseActivity {


    @Bind(R.id.activity_want_to_feedBack)
    LinearLayout activity_want_to_feedBack;
    @Bind(R.id.iv_abnormal)
    ImageView iv_abnormal;//功能异常

    @Bind(R.id.iv_experience)
    ImageView iv_experience;//体验问题

    @Bind(R.id.iv_newfunction)
    ImageView iv_newfunction;//新功能

    @Bind(R.id.iv_other)
    ImageView iv_other;//其他

    @Bind(R.id.et_content)
    EditText et_content;//描述
    @Bind(R.id.tv_words_num)
    TextView tv_words_num;
    private int maxWordsNum = 5000; //输入限制的最大字数

    @Bind(R.id.iv_addPic)
    ImageView iv_addPic;//添加照片1
    private MyCallBackTakePhoto myCallBackTakePhoto;
    private int TAG = 0;//  标记是哪张图片
    private int countPic = 0;// 添加图片数量  imageview处图片是否为空
    private String namePic1 = "";
    private String namePic2 = "";
    private String namePic3 = "";
    private String namePic4 = "";
    @Bind(R.id.iv_pic_1)
    ImageView iv_pic_1;
    @Bind(R.id.iv_pic_2)
    ImageView iv_pic_2;
    @Bind(R.id.iv_pic_3)
    ImageView iv_pic_3;
    @Bind(R.id.iv_pic_4)
    ImageView iv_pic_4;
    private File pic_a;
    private File pic_b;
    private File pic_c;
    private File pic_d;
    private PictureSelectPopupWindow pictureSelectPopupWindow;
    private PictureHandlePopupWindow pictureHandlePopupWindow;


    @Bind(R.id.tv_commit)
    TextView tv_commit;
    private boolean abnormal = true;
    private boolean experience = true;
    private boolean newfunction = true;
    private boolean other = true;
    private int category_id = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_call_back);
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_pic_select, null);
        myCallBackTakePhoto = MyCallBackTakePhoto.of(contentView);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initTitleBar(R.string.txt_mycallback);
        setRightTextVisibility(View.GONE);
        tv_words_num.setText("0/" + maxWordsNum);
        et_content.addTextChangedListener(new TextWatcher() {
            CharSequence temp;
            int selectionStart;
            int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_words_num.setText(s.length() + "/" + maxWordsNum);
                selectionStart = et_content.getSelectionStart();
                selectionEnd = et_content.getSelectionEnd();
                if (temp.length() > maxWordsNum) {
//                    toast("不能超过" + maxWordsNum + "个字");
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    et_content.setText(s);
                    et_content.setSelection(tempSelection); //设置光标在最后
                }
            }
        });
        pictureSelectPopupWindow = new PictureSelectPopupWindow(this, pictureSelectItemsOnClick);
        pictureHandlePopupWindow = new PictureHandlePopupWindow(this, pictureHandleItemsOnClick);

        pictureSelectPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                pictureSelectPopupWindow.backgroundAlpha(1.0f);
            }
        });
        pictureHandlePopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                pictureHandlePopupWindow.backgroundAlpha(1.0f);
            }
        });
    }

    @Override
    public void initListener() {
        iv_abnormal.setOnClickListener(this);
        iv_experience.setOnClickListener(this);
        iv_newfunction.setOnClickListener(this);
        iv_other.setOnClickListener(this);
        iv_addPic.setOnClickListener(this);
        tv_commit.setOnClickListener(this);
        iv_pic_1.setOnClickListener(this);
        iv_pic_2.setOnClickListener(this);
        iv_pic_3.setOnClickListener(this);
        iv_pic_4.setOnClickListener(this);

    }

    View.OnClickListener pictureSelectItemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_pick_photo:   // 调用系统相机
                    myCallBackTakePhoto.onClick(view, getTakePhoto());
                    pictureSelectPopupWindow.dismiss();
                    break;
                case R.id.btn_take_photo:   // 调用系统相机
                    myCallBackTakePhoto.onClick(view, getTakePhoto());
                    pictureSelectPopupWindow.dismiss();
                    break;
                case R.id.btn_cancel:   // 取消
                    pictureSelectPopupWindow.dismiss();
                    break;
            }
        }
    };

    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        Dlog.e("GG" + result.getImage().getCompressPath());
        addImages(result.getImage().getCompressPath());
    }

    View.OnClickListener pictureHandleItemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_big_picture:
                    pictureHandlePopupWindow.dismiss();
                    Bundle bundle = new Bundle();
                    if (TAG == 1) {
                        bundle.putString("img_path", namePic1);
                        JumpItent.jump(MyCallBackActivity.this, ImageShowerActivity.class, bundle);
                    } else if (TAG == 2) {
                        bundle.putString("img_path", namePic2);
                        JumpItent.jump(MyCallBackActivity.this, ImageShowerActivity.class, bundle);
                    } else if (TAG == 3) {
                        bundle.putString("img_path", namePic3);
                        JumpItent.jump(MyCallBackActivity.this, ImageShowerActivity.class, bundle);
                    } else if (TAG == 4) {
                        bundle.putString("img_path", namePic4);
                        JumpItent.jump(MyCallBackActivity.this, ImageShowerActivity.class, bundle);
                    }
                    break;
                case R.id.btn_delete: // 删除图片
                    pictureHandlePopupWindow.dismiss();
                    deleteImage();
                    break;
                case R.id.btn_cancel:
                    pictureHandlePopupWindow.dismiss();
                    break;

            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_commit:
                doCommit();
                break;
            case R.id.iv_abnormal:
                if (abnormal) {
                    category_id = 1;
                    iv_abnormal.setImageResource(R.drawable.ic_abnormal_pressed);
                    iv_experience.setImageResource(R.drawable.ic_experience);
                    iv_newfunction.setImageResource(R.drawable.ic_newfunction);
                    iv_other.setImageResource(R.drawable.ic_other);
                    abnormal = false;
                    experience = true;
                    newfunction = true;
                    other = true;
                } else {
                    category_id = 0;
                    iv_abnormal.setImageResource(R.drawable.ic_abnormal);
                    abnormal = true;
                }
                break;
            case R.id.iv_experience:
                if (experience) {
                    category_id = 2;
                    iv_abnormal.setImageResource(R.drawable.ic_abnormal);
                    iv_experience.setImageResource(R.drawable.ic_experience_pressed);
                    iv_newfunction.setImageResource(R.drawable.ic_newfunction);
                    iv_other.setImageResource(R.drawable.ic_other);
                    abnormal = true;
                    experience = false;
                    newfunction = true;
                    other = true;
                } else {
                    category_id = 0;
                    iv_experience.setImageResource(R.drawable.ic_experience);
                    experience = true;
                }
                break;
            case R.id.iv_newfunction:
                if (newfunction) {
                    category_id = 3;
                    iv_abnormal.setImageResource(R.drawable.ic_abnormal);
                    iv_experience.setImageResource(R.drawable.ic_experience);
                    iv_newfunction.setImageResource(R.drawable.ic_newfunction_pressed);
                    iv_other.setImageResource(R.drawable.ic_other);
                    abnormal = true;
                    experience = true;
                    newfunction = false;
                    other = true;
                } else {
                    category_id = 0;
                    iv_newfunction.setImageResource(R.drawable.ic_newfunction);
                    newfunction = true;
                }
                break;
            case R.id.iv_other:
                if (other) {
                    category_id = 4;
                    iv_abnormal.setImageResource(R.drawable.ic_abnormal);
                    iv_experience.setImageResource(R.drawable.ic_experience);
                    iv_newfunction.setImageResource(R.drawable.ic_newfunction);
                    iv_other.setImageResource(R.drawable.ic_other_pressed);
                    abnormal = true;
                    experience = true;
                    newfunction = true;
                    other = false;
                } else {
                    category_id = 0;
                    iv_other.setImageResource(R.drawable.ic_other);
                    other = true;
                }
                break;
            case R.id.iv_addPic:
                pictureSelectPopupWindow.showAtLocation(findViewById(R.id.activity_want_to_feedBack), Gravity.BOTTOM, 0, 0);
                pictureSelectPopupWindow.backgroundAlpha(0.5f);
                break;
            case R.id.iv_pic_1:
                TAG = 1;
                pictureHandlePopupWindow.showAtLocation(findViewById(R.id.activity_want_to_feedBack), Gravity.BOTTOM, 0, 0);
                pictureHandlePopupWindow.backgroundAlpha(0.5f);
                break;
            case R.id.iv_pic_2:
                TAG = 2;
                pictureHandlePopupWindow.showAtLocation(findViewById(R.id.activity_want_to_feedBack), Gravity.BOTTOM, 0, 0);
                pictureHandlePopupWindow.backgroundAlpha(0.5f);
                break;
            case R.id.iv_pic_3:
                TAG = 3;
                pictureHandlePopupWindow.showAtLocation(findViewById(R.id.activity_want_to_feedBack), Gravity.BOTTOM, 0, 0);
                pictureHandlePopupWindow.backgroundAlpha(0.5f);
                break;
            case R.id.iv_pic_4:
                TAG = 4;
                pictureHandlePopupWindow.showAtLocation(findViewById(R.id.activity_want_to_feedBack), Gravity.BOTTOM, 0, 0);
                pictureHandlePopupWindow.backgroundAlpha(0.5f);
                break;
        }
    }

    /**
     * 提交
     */
    private void doCommit() {
        String commitContent = et_content.getText().toString().trim();
        if (TextUtils.isEmpty(commitContent)) {
            Toastutils.show("反馈内容不能为空");
        } else if(category_id == 0){
            Toastutils.show("请选择问题类型");
        }else{
            pic_a = new File(namePic1);
            pic_b = new File(namePic2);
            pic_c = new File(namePic3);
            pic_d = new File(namePic4);
            showLoadingBar("");
            Dlog.e(category_id + "");
            RequestParams params = new RequestParams(Config.commit_callback);
            params.addBodyParameter("id_auth", user.getId_auth());
            params.addBodyParameter("feedback_content", commitContent);
            params.addBodyParameter("category_id", category_id + "");
            if (pic_a.isFile()) {
                Dlog.e(pic_a.getPath());
                params.addBodyParameter("pic1", pic_a);
            }
            if (pic_b.isFile()) {
                Dlog.e(pic_b.getPath());
                params.addBodyParameter("pic2", pic_b);
            }
            if (pic_c.isFile()) {
                Dlog.e(pic_c.getPath());
                params.addBodyParameter("pic3", pic_c);
            }
            if (pic_d.isFile()) {
                Dlog.e(pic_d.getPath());
                params.addBodyParameter("pic4", pic_d);
            }
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    Dlog.e(s);
                    int errCode = JsonUtils.getErrCode(s);
                    String data = JsonUtils.getData(s);
                    String errMsg = JsonUtils.getErrMsg(s);
                    if (errCode == 200) {
                        Toastutils.show(errMsg);
                        finish();
                    }
                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onFinished() {
                    hideLoadingBar();
                }
            });
        }

    }


    /**
     * 添加图片
     *
     * @param path
     */
    public void addImages(String path) {
        if (countPic == 0) {
            Dlog.e(countPic + "namePic1" + path);
            if (TextUtils.isEmpty(namePic1)) {
                namePic1 = path;
                iv_pic_1.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_1, namePic1);
                countPic += 1;
            }
        } else if (countPic == 1) {
            if (TextUtils.isEmpty(namePic1)) {
                namePic1 = path;
                iv_pic_1.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_1, namePic1);
                countPic += 1;
            } else if (TextUtils.isEmpty(namePic2)) {
                namePic2 = path;
                iv_pic_2.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_2, namePic2);
                countPic += 1;
            } else if (TextUtils.isEmpty(namePic3)) {
                namePic3 = path;
                iv_pic_3.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_3, namePic3);
                countPic += 1;
            } else if (TextUtils.isEmpty(namePic4)) {
                namePic4 = path;
                iv_pic_4.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_4, namePic4);
                countPic += 1;
            }
        } else if (countPic == 2) {
            if (TextUtils.isEmpty(namePic1)) {
                namePic1 = path;
                iv_pic_1.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_1, namePic1);
                countPic += 1;
            } else if (TextUtils.isEmpty(namePic2)) {
                namePic2 = path;
                iv_pic_2.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_2, namePic2);
                countPic += 1;
            } else if (TextUtils.isEmpty(namePic3)) {
                namePic3 = path;
                iv_pic_3.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_3, namePic3);
                countPic += 1;
            } else if (TextUtils.isEmpty(namePic4)) {
                namePic4 = path;
                iv_pic_4.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_4, namePic4);
//                iv_add_pic.setVisibility(View.GONE);
                countPic += 1;
            }
        } else if (countPic == 3) {
            if (TextUtils.isEmpty(namePic1)) {
                namePic1 = path;
                iv_pic_1.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_1, namePic1);
                iv_addPic.setVisibility(View.GONE);
                countPic += 1;
            } else if (TextUtils.isEmpty(namePic2)) {
                namePic2 = path;
                iv_pic_2.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_2, namePic2);
                iv_addPic.setVisibility(View.GONE);
                countPic += 1;
            } else if (TextUtils.isEmpty(namePic3)) {
                namePic3 = path;
                iv_pic_3.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_3, namePic3);
                iv_addPic.setVisibility(View.GONE);
                countPic += 1;
            } else if (TextUtils.isEmpty(namePic4)) {
                namePic4 = path;
                iv_pic_4.setVisibility(View.VISIBLE);
                x.image().bind(iv_pic_4, namePic4);
                countPic += 1;
                iv_addPic.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 删除图片
     */
    private void deleteImage() {
        if (TAG == 1) {
            namePic1 = "";
            iv_pic_1.setVisibility(View.GONE);
            iv_addPic.setVisibility(View.VISIBLE);
            iv_addPic.setImageResource(R.drawable.ic_addpic);
            countPic -= 1;
        } else if (TAG == 2) {
            namePic2 = "";
            iv_pic_2.setVisibility(View.GONE);
            iv_addPic.setVisibility(View.VISIBLE);
            iv_addPic.setImageResource(R.drawable.ic_addpic);
            countPic -= 1;
        } else if (TAG == 3) {
            namePic3 = "";
            iv_pic_3.setVisibility(View.GONE);
            iv_addPic.setVisibility(View.VISIBLE);
            iv_addPic.setImageResource(R.drawable.ic_addpic);
            countPic -= 1;
        } else if (TAG == 4) {
            namePic4 = "";
            iv_pic_4.setVisibility(View.GONE);
            iv_addPic.setVisibility(View.VISIBLE);
            iv_addPic.setImageResource(R.drawable.ic_addpic);
            countPic -= 1;
        }
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (data != null) {
//            if (resultCode == RESULT_CODE) {
//                et_content.setText("");
//                iv_add_pic.setVisibility(View.VISIBLE);
//                iv_pic_1.setVisibility(View.GONE);
//                namePic1 = "";
//                iv_pic_2.setVisibility(View.GONE);
//                namePic1 = "";
//                iv_pic_3.setVisibility(View.GONE);
//                namePic1 = "";
//                iv_pic_4.setVisibility(View.GONE);
//                namePic1 = "";
//            }
//        }
//
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
